package com.frxuexi.task.view

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Button
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.Task
import com.frxuexi.task.R
import com.frxuexi.task.intent.TaskIntent
import com.frxuexi.task.viewmodel.TaskViewModel
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat

@Composable
fun TaskDetailPage(navHostController:NavHostController,json:String,vm:TaskViewModel= hiltViewModel()) {
    val kv = MMKV.defaultMMKV()

    val name by remember {
        mutableStateOf(kv.decodeString("name", "未登录"))
    }
    val role by remember {
        mutableStateOf(kv.decodeInt("role", 0))
    }

    var status by remember {
        mutableStateOf("同意")
    }
    val task = Gson().fromJson(json, Task::class.java)

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*>->{
                        Log.i("===","审批完成")
                        navHostController.popBackStack()
                    }
                    else->{}
                }
            }

        }
    })
    Column {
        MyTitleBar(title = "任务审批", navHostController = navHostController)
        Column(Modifier.padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)) {
            Text(text = "任务申报人信息", color = Color.Blue)
            ElevatedCard(
                modifier = Modifier.fillMaxWidth(),
                colors = CardDefaults.elevatedCardColors(containerColor = Color.White)
            ) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(10.dp)
                ) {
                    AsyncImage(
                        model = "https://img1.baidu.com/it/u=3406115504,1068597125&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1725037200&t=3c5e09c88f402ee3f67ff412d5dae000",
                        contentDescription = "",
                        modifier = Modifier
                            .size(80.dp)
                            .clip(CircleShape),
                        contentScale = ContentScale.Crop
                    )
                    Spacer(modifier = Modifier.width(10.dp))
                    Column(
                        modifier = Modifier.height(80.dp),
                        verticalArrangement = Arrangement.SpaceAround
                    ) {
                        Text(text = task.uname!!)
                        Text("产品经理")
                    }
                }
            }
            Text(text = "任务", color = Color.Blue)
            Text(text = "任务名称：${task.name}")
            Text(text = "任务详情：${task.desp}")

            Spacer(modifier = Modifier.height(10.dp))

            Row(Modifier.padding(10.dp)) {
                Column(horizontalAlignment = Alignment.CenterHorizontally) {
                    AsyncImage(
                        model = "https://img1.baidu.com/it/u=3406115504,1068597125&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1725037200&t=3c5e09c88f402ee3f67ff412d5dae000",
                        contentDescription = "",
                        modifier = Modifier
                            .size(40.dp)
                            .clip(CircleShape),
                        contentScale = ContentScale.Crop
                    )
                    Divider(
                        Modifier
                            .height(30.dp)
                            .width(1.dp)
                    )
                    val id =
                        if (task.status == "同意") R.drawable.ic_action_yes else if (task.status == "取消") R.drawable.ic_action_refuse else R.drawable.ic_action_commit
                    Image(
                        painter = painterResource(id = id), contentDescription = "",
                        modifier = Modifier
                            .size(40.dp),
                        contentScale = ContentScale.Crop
                    )
                }
                Spacer(modifier = Modifier.width(10.dp))
                Column {
                    Text(text = "已发布")
                    Text(text = "申报时间：${task.time}", color = Color.Gray, fontSize = 12.sp)
                    Spacer(modifier = Modifier.height(30.dp))
                    val state = if (task.status == "已提交") "审批中" else task.status
                    Text(text = state!!)
                }
            }

            Spacer(modifier = Modifier.height(10.dp))

            Text(text = "审批意见", color = Color.Blue)
            Divider()
            Row(verticalAlignment = Alignment.CenterVertically) {
                Text(text = "意见内容:")
                Spacer(modifier = Modifier.width(10.dp))
                RadioButton(selected = status=="同意", onClick = { status="同意" })
                Text(text = "同意")
                Spacer(modifier = Modifier.width(10.dp))
                RadioButton(selected = status=="取消", onClick = { status="取消" })
                Text(text = "取消")
            }

            Button(modifier = Modifier.fillMaxWidth(), onClick = {
                task.status=status
                vm.sendIntent(TaskIntent.approveTask(task))
            }) {
                Text(text = "提交")
            }
        }
    }
}